package com.park.controller;

import com.park.common.bean.ArticleInputView;
import com.park.common.bean.PageBean;
import com.park.common.bean.ResponseBean;
import com.park.common.constant.IDBConstant;
import com.park.common.exception.MessageException;
import com.park.common.poo.ArticleInfo;
import com.park.common.util.JsonUtils;
import com.park.platform.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("article")
public class ArticleController extends BaseController {

    @Autowired
    private IArticleService articleService;

    @RequestMapping(value = "getArticles")
    public String getArticles(ArticleInputView articleInputView, Model model) {
        try {
            model.addAllAttributes(JsonUtils.fromJsonDF(articleInputView));
            PageBean pageBean = articleService.getArticlesIN(articleInputView);
            super.setPageInfo(model, pageBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Article/ArticleList";
    }

    @RequestMapping("viewArticle")
     public String viewArticle(Integer articleId, Model model){
        if(articleId != null) {
            model.addAllAttributes(JsonUtils.fromJsonDF(articleService.getArticleInfo(articleId)));
        }
        return "Article/ArticleViews";
    }

    @RequestMapping(value = "getNews")
    public String getNews(ArticleInputView articleInputView, Model model) {
        try {
            articleInputView.setServiceType(IDBConstant.LOGIC_STATUS_NO);
            model.addAllAttributes(JsonUtils.fromJsonDF(articleInputView));
            PageBean pageBean = articleService.getArticlesIN(articleInputView);
            super.setPageInfo(model, pageBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "News/NewsList";
    }

    @RequestMapping("viewNews")
    public String viewNews(Integer articleId, Model model){
        if(articleId != null) {
            model.addAllAttributes(JsonUtils.fromJsonDF(articleService.getArticleInfo(articleId)));
        }
        return "News/NewsViews";
    }

    /**
     * 保存或者更新商品的信息, 包括商品的图片上传
     * @param multipartRequest
     * @return
     */
    @ResponseBody
    @RequestMapping("saveArticle")
    public ResponseBean saveArticle(ArticleInfo activityInfo, MultipartHttpServletRequest multipartRequest){
        try {
            Integer articleId = articleService.saveArticle(activityInfo, multipartRequest);
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("articleId", articleId);
            return new ResponseBean(data);
        } catch (MessageException e) {
            e.printStackTrace();
            return new ResponseBean(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseBean(false);
        }
    }

    @ResponseBody
    @RequestMapping("deleteArticle")
    public ResponseBean deleteArticle(int articleId){
        try {
            articleService.newDeleteArticle(articleId);
            return new ResponseBean(true);
        } catch (MessageException e) {
            e.printStackTrace();
            return new ResponseBean(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseBean(false);
        }
    }

}
